var select_id="";
var status = "";
$(document).ready(function() { //data_tbody brief
    $("#brief").click(function (){
        $("#list_mode").removeClass("data_tbody");
        $("#list_mode").addClass("data_tbody brief");
        $("#full").removeClass("active");
        $("#brief").addClass("active");
    });
    $("#full").click(function (){
        $("#list_mode").removeClass("data_tbody brief");
        $("#list_mode").addClass("data_tbody");
        $("#brief").removeClass("active");
        $("#full").addClass("active");
    });
    $("#search_bt").click(function (){
        if ($("#keyword").val()=="Search by Keywords..."){
            $("#keyword").val("");
        }
        $('#search').submit()
    });
    $("#keyword").focus(function () {
        if ($("#keyword").val()=="Search by Keywords..."){
            $("#keyword").val("");
        }
    });
    $("#keyword").blur(function () {
        if ($("#keyword").val()==""){
            $("#keyword").val("Search by Keywords...");
        }
    });
    $("#viewdialog").click(function (){
        if (select_id == ""){
            alert("please select one to view!");
            return false;
        }else{
            window.location.href="view.html?id="+select_id+"&from=list";
        }
    });
    $("#editdialog").click(function (){
        if (select_id == ""){
            alert("please select one to edit!");
            return false;
        }else{
            $.post('checkdialog.html', {
                id: select_id
            },
            function(data) {
                if (data != ""){
                    $('.error h3').html(data);
                    return false;
                }else{
                    window.location.href="edit.html?id="+select_id+"&from=list";
                }
            });
							
        }
    });
    $("#newversiondialog").click(function (){
        if (select_id == ""){
            alert("please select one to create new version dialog!");
            return false;
        }else{
            if (status == "retired"){
                $('.error h3').html("<font color='red'>This action not available; Dialog is in Retired Status</font>");
                return false;
            }else{
                window.location.href="newversion.html?id="+select_id;
            } 
								
        }
    });
    $("#copydialog").click(function (){
        if (select_id == ""){
            alert("please select one to copy!");
            return false;
        }else{
            window.location.href="copy.html?id="+select_id;
        }
    });
    $("#exportXMI").click(function (){
        if (select_id == ""){
            alert("please select one to Export!");
            return false;
        }else{
            window.location.href="exportxmi.html?id="+select_id;
        }
    });
    $("#retiredialog").click(function (){
        if (select_id == ""){
            alert("please select one to retire!");
            return false;
        }else{
            if (status == "retired"){
                $('.error h3').html("<font color='red'>This action not available; Dialog is in Retired Status</font>");
                return false;
            }else{
                window.location.href="retire.html?id="+select_id;
            } 

        }
    });
    
    $("#publishdialog").click(function (){
        if (select_id == ""){
            alert("please select one to publish!");
            return false;
        }else{
            $.post('checkdialog.html', {
                id: select_id
            },
            function(data) {
                if (data != ""){
                    $('.error h3').html(data);
                    return false;
                }else{
                    window.location.href="publish.html?id="+select_id;
                }
            });

        }
    });
    $("#deletedialog").click(function (){
        var imageStatus = $(this).children().attr('src');        
        if (imageStatus.indexOf("_light")>-1){
            $.post('delete.html', {
                    id: select_id
                },
                function(data) {
                    if (data != ""){
                        if (data.indexOf(":true")>0){
                            data = data.replace(":true","");
                            $("#" + select_id).remove();                            
                        }
                        $('.error h3').html(data);
                        return false;
                    }else{
                        $('.error h3').html("error!");
                    }
                });
            return false;
        }
        if (select_id == ""){
            alert("please select one to delete!");
            return false;
        }else{
            if (confirm("Are you sure you want to delete this dialog?")){
                $.post('delete.html', {
                    id: select_id
                },
                function(data) {
                    if (data != ""){
                        if (data.indexOf(":true")>0){
                            data = data.replace(":true","");
                            $("#" + select_id).remove();
                            select_id = "";
                        }
                        $('.error h3').html(data);
                        return false;
                    }else{
                        $('.error h3').html("error!");
                    }
                });
            }else{
                $('.error h3').html("Action cancelled");
            }

        }
    });

    $("#copydialog").click(function() {
        if (select_id == "") {
            alert("please select one to copy!");
        } else {
            window.location.href="copy.html?id=" + select_id;
        }
    });

    $("#replicatedialog").click(function() {
        if (select_id == "") {
            alert("please select one to replicate!");
        } else {
            window.location.href="replicate.html?id=" + select_id;
        }
    });
});
var Active = function(obj,qid,qStatus){
    $("div.data_tbody div").removeClass("active")
    var FirstFlag = $(obj).attr("class");
    if (select_id==qid){
        select_id="";
        status = "";
        displayButton($("#editdialog"));
        displayButton($("#newversiondialog"));       
        displayButton($("#retiredialog"));
        displayButton($("#publishdialog"));
        displayButton($("#deletedialog"));
    }else{
        select_id=qid;
        status = qStatus;
        $(obj).toggleClass("active");
        if (qStatus == "inprocess"){
            displayButton($("#editdialog"));
            displayButton($("#newversiondialog"));            
            displayButton($("#retiredialog"));
            displayButton($("#publishdialog"));
            displayButton($("#deletedialog"));
        }
        if (qStatus == "published"){
            displayButton($("#retiredialog"));
            greyButton($("#editdialog"));
            greyButton($("#deletedialog"));
            greyButton($("#publishdialog"));
        }
        if (qStatus == "retired"){
            greyButton($("#editdialog"));
            greyButton($("#newversiondialog"));
            greyButton($("#retiredialog"));
            greyButton($("#deletedialog"));
            greyButton($("#publishdialog"));
        }
    }
}